package org.jeecg.modules.crawler.job;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.crawler.controller.NewCrawlerController;
import org.jeecg.modules.crawler.entity.NewCrawlerInfo;
import org.jeecg.modules.crawler.entity.NewCrawlerKeyword;
import org.jeecg.modules.crawler.service.INewCrawlerInfoService;
import org.jeecg.modules.crawler.service.INewCrawlerKeywordService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务定时发送
 */
@Slf4j
public class SeuCrawlerJob implements Job {

	@Autowired
	private INewCrawlerInfoService newCrawlerInfoService;

	@Autowired
	private INewCrawlerKeywordService newCrawlerKeywordService;


	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		WebClient webClient = getClient();
		String result = null;
		List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();

		try {
			result = getHtml("https://www.seu.edu.cn/ddyw_47245/list.htm", webClient);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Document doc = Jsoup.parse(result);
		Elements elementsByClass = doc.getElementsByClass("news_list list2");
		for (Element byClass : elementsByClass) {
			Elements children = byClass.children();
			for(Element child : children){
				Elements elements =  child.getElementsByTag("a");
				for(Element element : elements){
					String title = element.getElementsByClass("news_title").text();
					String meta = element.getElementsByClass("news_meta").text();
					System.out.println(title+"   "+meta);
					NewCrawlerInfo crawlerInfo = new NewCrawlerInfo();
					crawlerInfo.setTitle(title);
					crawlerInfo.setTime(meta);
//					crawlerInfos.add(crawlerInfo);
					try {
						newCrawlerInfoService.saveOrUpdate(crawlerInfo);
					}catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}
//		newCrawlerInfoService.saveOrUpdateBatch(crawlerInfos);

//		List<NewCrawlerKeyword> keywordList = newCrawlerKeywordService.list();
//
//		try {
//			for (NewCrawlerKeyword newCrawlerKeyword : keywordList) {
//				for (int i = 1; i < 6; i++){
//					String queryStr = "[{\"field\":\"pageIndex\",\"value\":"+i+"},{\"field\":\"group\",\"value\":0},{\"field\":\"searchType\",\"value\":\"\"},{\"field\":\"keyword\",\"value\":\"" + newCrawlerKeyword.getKeyword() + "\"},{\"field\":\"recommend\",\"value\":\"1\"},{\"field\":4,\"value\":\"\"},{\"field\":5,\"value\":\"\"},{\"field\":6,\"value\":\"\"},{\"field\":7,\"value\":\"\"}]";
//					String query = Base64Encoder.encode(queryStr.getBytes(StandardCharsets.UTF_8));
//					List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();
//					Map<String, Object> fromMap = new HashMap<>();
//					fromMap.put("searchInfo", query);
//					HttpRequest post = HttpUtil.createPost("https://www.seu.edu.cn/_web/_search/api/searchCon/create.rst?_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__&tt=" + Math.random());
//					post.header("Referer", "https://www.seu.edu.cn/_web/_search/api/search/new.rst?locale=zh_CN&request_locale=zh_CN&_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__");
//					post.form(fromMap);
//					HttpResponse httpResponse = post.execute();
//					JSONObject respObj = (JSONObject) JSONObject.parse(httpResponse.body());
//					String respStr = respObj.getString("data");
//					String xpath = "//html/body/div";
//					JXDocument jxDocument = new JXDocument(respStr);
//					List<Object> rs = jxDocument.sel(xpath);
//					for (Object o : rs) {
//						if (o instanceof Element) {
//							NewCrawlerInfo tempInfo = new NewCrawlerInfo();
//							//链接
//							String aStr = ((Element) o).outerHtml();
//							String apath = "//html/body/div/h3/a/@href";
//							JXDocument aJxDocument = new JXDocument(aStr);
//							List<Object> ars = aJxDocument.sel(apath);
////                        System.out.println(ars.get(0).toString());
//							tempInfo.setLink(ars.get(0).toString());
//							//标题
//							String titleStr = ((Element) o).outerHtml();
//							String titlepath = "//html/body/div/h3/a";
//							JXDocument titleJxDocument = new JXDocument(titleStr);
//							List<Object> titlers = titleJxDocument.sel(titlepath);
//							Object titleO = titlers.get(0);
//							String title = ((Element) titleO).html();
////                        System.out.println(title);
//							tempInfo.setTitle(title);
//							tempInfo.setId(title);
//							//其他信息 创建者，发布时间，目录 ，出处
//							String otherStr = ((Element) o).outerHtml();
//							String otherpath = "//html/body/div/span";
//							JXDocument otherJxDocument = new JXDocument(otherStr);
//							List<Object> otherrs = otherJxDocument.sel(otherpath);
//							if(otherrs.size()>=1){
//								Object otherO1 = otherrs.get(0);
//								tempInfo.setAuthor(((Element) otherO1).html());
//							}
//							if(otherrs.size()>=2) {
//								Object otherO2 = otherrs.get(1);
//								tempInfo.setTime(((Element) otherO2).html().replaceAll("发布时间:", ""));
//							}
//							if(otherrs.size()>=3) {
//								Object otherO3 = otherrs.get(2);
//								tempInfo.setMl(((Element) otherO3).html());
//							}
//							if(otherrs.size()>=4) {
//								Object otherO4 = otherrs.get(3);
//								tempInfo.setSource(((Element) otherO4).html());
//							}
//							crawlerInfos.add(tempInfo);
//						}
//					}
//					newCrawlerInfoService.saveOrUpdateBatch(crawlerInfos);
//				}
//			}
//		} catch (Exception var7) {
//			var7.printStackTrace();
//		}

	}



	public  static WebClient  getClient(){
		WebClient  webClient = new WebClient(BrowserVersion.CHROME);

		webClient.getOptions().setThrowExceptionOnScriptError(false);
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		webClient.getOptions().setActiveXNative(false);
		webClient.getOptions().setCssEnabled(false);
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.getOptions().setTimeout(30000);
		webClient.setJavaScriptTimeout(30000);
		webClient.getOptions().setRedirectEnabled(true);
		return  webClient;

	}


	public  static String  getHtml(String url, WebClient webClient) throws InterruptedException {
		HtmlPage page = null;
		try {
			webClient.getOptions().setUseInsecureSSL(true);
			page = webClient.getPage(url);
		} catch (Exception e) {
			e.printStackTrace();
			webClient.close();

		}

		//该方法阻塞线程
		webClient.waitForBackgroundJavaScript(30000);
		return   page.asXml();
	}


	public static void main(String[] args) {
		WebClient webClient = getClient();
		String result = null;
		List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();

		try {
			webClient.addRequestHeader(":authority","www.seu.edu.cn");
			webClient.addRequestHeader(":scheme","https");
			webClient.addRequestHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
			webClient.addRequestHeader("accept-encoding","gzip, deflate, br, zstd");
			webClient.addRequestHeader("priority","u=0, i");
			webClient.addRequestHeader("upgrade-insecure-requests","1");
			webClient.addRequestHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0");

			result = getHtml("https://www.seu.edu.cn/ddyw_47245/list.htm", webClient);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Document doc = Jsoup.parse(result);
		Elements elementsByClass = doc.getElementsByClass("news_list list2");
		for (Element byClass : elementsByClass) {
			Elements children = byClass.children();
			for(Element child : children){
				Elements elements =  child.getElementsByTag("a");
				for(Element element : elements){
					String title = element.getElementsByClass("news_title").text();
					String meta = element.getElementsByClass("news_meta").text();
					System.out.println(title+"   "+meta);
					NewCrawlerInfo crawlerInfo = new NewCrawlerInfo();
					crawlerInfo.setTitle(title);
					crawlerInfo.setTime(meta);
//					crawlerInfos.add(crawlerInfo);
					try {
//						newCrawlerInfoService.saveOrUpdate(crawlerInfo);
					}catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}
	}

}
